//
//  SecenddViewController.m
//  SelectionForYou
//
//  Created by qianfeng on 15-6-11.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "SecenddViewController.h"
#import <WebKit/WebKit.h>
#import "MMProgressHUD.h"
#import "MMLinearProgressView.h"
#import "MyControl.h"
#import "MyModel.h"
#import "DBManager.h"
#import <MessageUI/MessageUI.h>
#define kSubUrl @"http://jingxuan.guokr.com/pick/v2/%ld/"
@interface SecenddViewController ()<UIWebViewDelegate,UIActionSheetDelegate,MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>
{
   
    UIWebView *_webView;
   
}

@property(nonatomic,strong) UIBarButtonItem *ietm2;
@property(nonatomic,strong) MyModel *model;
@property(nonatomic,strong) NSTimer *timer;
@property(nonatomic) UIWebView *webView;
@end

@implementation SecenddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _model=[[MyModel alloc] init];
    _dataArr=[[NSArray alloc ] init];

    UIBarButtonItem *ietm=[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAction target:self action:@selector(btnClick:)];
    ietm.tag=102;


    
    
    self.navigationItem.rightBarButtonItems = @[ietm];
    [self creatWebView];
    
    
    
    
    
    
}
- (void)btnClick:(UIButton *)btn{
    
    switch (btn.tag) {

        case 102:
        {
            [self shardData];

        }
            break;
            
        default:
            break;
    }
    
  
    
    
    
    
}
- (void)creatWebView{
//    self.automaticallyAdjustsScrollViewInsets=NO;
   
    
   
         _webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-30)];
    
   
    _webView.delegate=self;
    _webView.scalesPageToFit = YES;
    
    //_webView.allowsBackForwardNavigationGestures=YES;
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:kSubUrl,(long)self.idd.integerValue]]];
    
    [_webView loadRequest:request];
    
//    UIImageView *imageview=[[UIImageView alloc] init];
//    CGFloat y=_webView.frame.size.height;
//    imageview.frame=CGRectMake(0, 0, kScreenSize.width, 40);
//    NSLog(@"%f",y);
//    imageview.image=[UIImage imageNamed: @"wight"];
//    [_webView addSubview:imageview];
    _webView.backgroundColor=[UIColor grayColor];
   
    _webView.scrollView.bounces=NO;
   
    [self.view addSubview:_webView];
   
}

//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//    NSLog(@"1");
//    UIImageView *imageview=[[UIImageView alloc] init];
//    CGFloat y=_webView.frame.size.height;
//    imageview.frame=CGRectMake(0, 570, kScreenSize.width, 100);
//    NSLog(@"%f",y);
//    imageview.image=[UIImage imageNamed: @"wight"];
//    [_webView addSubview:imageview];
//
//}
- (void)shardData{
    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
        UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"分享" message:@"分享一些数据" preferredStyle:UIAlertControllerStyleActionSheet];
        //取消按钮
        [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        
        /********************************************/
        //短信按钮
        [sheet addAction:[UIAlertAction actionWithTitle:@"短信分享" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            //点击按钮的行为
            
            /*
             *MFMessageComposeViewController内部具有发短信的功能模块
             */
            if ([MFMessageComposeViewController canSendText]) {
                //检测硬件 是否具备短信功能
                MFMessageComposeViewController *message = [[MFMessageComposeViewController alloc] init];
                //设置 联系人 （可以同时设置多个）
                message.recipients = @[];
                //设置标题
                message.subject = @"分享";
                //设置短信内容
                message.body = self.link_v2;
                NSLog(@"%@",message.body);
                //如果要处理 短信模块上的按钮
                //设置代理
                message.messageComposeDelegate = self;
                
                
                //还可以发送附件
                // message addAttachmentData:<#(NSData *)#> typeIdentifier:<#(NSString *)#> filename:<#(NSString *)#>
                
                //要用模态跳转 弹出这个视图控制器(message 就是一个导航)
                [self presentViewController:message animated:YES completion:nil];
                
            }else{
                NSLog(@"没有短信功能");
            }
            
        }]];
        /********************************************/
        
        //邮件按钮
        [sheet addAction:[UIAlertAction actionWithTitle:@"邮件分享" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            //邮件按钮的行为
            //检测应用是否支持邮件功能模块
            if ([MFMailComposeViewController canSendMail]) {
                MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
                //设置标题
                [mail setSubject:@"这里有你喜欢的东西哟"];
                
                //设置联系人
                [mail setToRecipients:@[]];
                //设置内容
                NSString *body = [NSString stringWithFormat:@"亲!点击有惊喜哟:%@",self.link_v2];
                NSLog(@"%@",body);
                //是否用html格式
                [mail setMessageBody:body isHTML:YES];
                  NSLog(@"%@",mail);
                //设置代理
                mail.mailComposeDelegate = self;
                //增加附件
                //把图片转化为二进制
                NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed: @"icon"]);
                
                [mail addAttachmentData:imageData mimeType:@"image/png" fileName:@"icon.png"];
                
                //模态跳转
                [self presentViewController:mail animated:YES completion:nil];
            }else{
                NSLog(@"没有邮件功能");
                UIAlertView *alter=[[UIAlertView alloc] initWithTitle:@"警告" message:@"您的邮件功能未打开" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alter show];
            }
            
            
        }]];
        /********************************************/
        
        //模态跳转 actionSheet
        [self presentViewController:sheet animated:YES completion:nil];
        
    }



}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled:
        {
            NSLog(@"被取消");
        }
            break;
        case MFMailComposeResultSaved:
        {
            NSLog(@"保存");
        }
            break;
        case MFMailComposeResultFailed:
        {
            NSLog(@"失败");
        }
            break;
        case MFMailComposeResultSent:
        {
            NSLog(@"发送");
        }
            break;
            
        default:
            break;
    }
    //模态跳转返回
    [controller dismissViewControllerAnimated:YES completion:nil];
    
}


#pragma mark - 短信 模块的协议

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
   
    switch (result) {
        case MessageComposeResultCancelled:
        {
            NSLog(@"短信被取消");
        }
            break;
        case MessageComposeResultSent:
        {
            NSLog(@"短信发送成功");
        }
            break;
        case MessageComposeResultFailed:
        {
            NSLog(@"短信失败");
        }
            break;
            
        default:
            break;
    }
   
    [controller dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - ios8之前的actionSheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            //短信
        {
                       
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"]];
        }
            break;
        case 1:
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:?12345678@qq.com"]];
        }
            break;
        case 2:
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];
        }
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//    NSString* url = request.URL.absoluteString;
//    NSLog(@"%@",url);
//    if ([url containsString:@"http://54.223.145.59:8090/cooperation/"]) {
//        return NO;
//    }
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
